<template>
  <ly-dialog
    v-model="visible"
    :title="$t('route.capitalProject') + '--' + '修改主信息'"
    width="500px"
    top="10vh"
    :confirm="handleConfirm"
    :cancel="handleCancel"
    :before-close="() => {$emit('update:visible', false)}"
    @open="handleOpen"
  >
    <ly-form
      ref="form"
      size="mini"
      label-width="80px"
      :model="temp"
    >
      <el-row>
        <el-col :span="20">
          <el-form-item
            label="投入日期"
            prop="orderDate"
          >
            <el-date-picker
              v-model="temp.orderDate"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="选择日期"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="20">
          <el-form-item
            label="任务名称"
            prop="itemName"
          >
            <el-input v-model="temp.itemName" placeholder="请填写" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="20">
          <el-form-item
            label="项目"
            prop="project"
          >
            <el-select
              v-model="temp.project"
              clearable
              placeholder="请选择项目"
              @change="handleChange($event,'project')"
            >
              <el-option
                v-for="item in projectList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="20">
          <el-form-item
            label="进展"
            prop="progress"
          >
            <el-select
              v-model="temp.progress"
              clearable
              placeholder="请选择进展"
              @change="handleChange($event,'progress')"
            >
              <el-option
                v-for="item in progressList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="20">
          <el-form-item
            label="类型"
            prop="type"
          >
            <el-select
              v-model="temp.type"
              clearable
              placeholder="请选择类型"
              @change="handleChange($event,'type')"
            >
              <el-option
                v-for="item in typeList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="20">
          <el-form-item
            label="投入金额"
            prop="investPrice"
          >
            <el-input v-model="temp.investPrice" type="number" placeholder="请填写" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="20">
          <el-form-item
            label="收益金额"
            prop="orderPrice"
          >
            <el-input v-model="temp.orderPrice" type="number" placeholder="请填写" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="20">
          <el-form-item
            label="备注"
            prop="remarks"
          >
            <el-input v-model="temp.remarks" placeholder="请填写" />
          </el-form-item>
        </el-col>
      </el-row>
    </ly-form>
  </ly-dialog>
</template>

<script>
import { updateOne } from '@/api/capital'

export default {
  name: 'ModifyMasterDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: undefined
    },
    multiple: {
      type: Array,
      default: undefined
    },
    typeList: {
      type: Array,
      default: undefined
    },
    projectList: {
      type: Array,
      default: undefined
    },
    progressList: {
      type: Array,
      default: undefined
    }
  },
  data() {
    return {
      temp: {
        itemName: '',
        orderDate: '',
        project: '',
        projectName: '',
        progress: '',
        progressName: '',
        type: '',
        typeName: '',
        investPrice: '',
        orderPrice: '',
        remarks: ''
      },
      restTemp: {
        itemName: '',
        orderDate: '',
        project: '',
        projectName: '',
        progress: '',
        progressName: '',
        type: '',
        typeName: '',
        investPrice: '',
        orderPrice: '',
        remarks: ''
      }
    }
  },
  methods: {
    async handleConfirm() {
      this.temp.cartonOrderNOs = this.multiple
      const list = this.temp.cartonOrderNOs.filter(item => { return item.checked === 1 })
      if (list.length > 0) {
        this.$confirm('检测到有审核的数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          const result = await updateOne('getCapitalBatchUpdate', this.temp)
          if (result.code === 200) {
            this.setVisible(false)
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
            this.$emit('add-data')
          }
        }).catch(() => {
        })
      } else {
        const result = await updateOne('getCapitalBatchUpdate', this.temp)
        if (result.code === 200) {
          this.setVisible(false)
          this.$notify({
            title: '成功',
            message: '修改成功',
            type: 'success',
            duration: 2000
          })
          this.$emit('add-data')
        }
      }
    },
    handleOpen() {
      this.temp = Object.assign({}, this.restTemp)
      if (this.multiple.length === 1) {
        this.temp = Object.assign({}, ...this.multiple)
      }
    },
    handleChange(id, val) {
      if (val === 'progress') {
        const list = this.progressList.filter(item => item.id === id)
        if (list.length > 0) {
          this.temp.progressName = list[0].name
        } else {
          this.temp.progressName = ''
        }
      } else if (val === 'type') {
        const list = this.typeList.filter(item => item.id === id)
        if (list.length > 0) {
          this.temp.typeName = list[0].name
        } else {
          this.temp.typeName = ''
        }
      } else {
        const list = this.projectList.filter(item => item.id === id)
        if (list.length > 0) {
          this.temp.projectName = list[0].name
        } else {
          this.temp.projectName = ''
        }
      }
    },
    handleCancel() {
      this.setVisible(false)
    },
    setVisible(flag) {
      this.$emit('update:visible', flag)
    }
  }
}
</script>

<style>

</style>
